package com.jeeplus.modules.guide.service;

import com.jeeplus.database.datasource.annotation.DS;
import com.jeeplus.modules.guide.mapper.WarningMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DS("gxnq")
@RequiredArgsConstructor
@Service
@Slf4j
public class WarningService {
    private final WarningMapper warningMapper;

    public String warning(){
        int count =0;
        List<Map<String,Object>> serial = warningMapper.findSerial();
        for (int i = 0; i < serial.size(); i++) {
            System.out.println(serial.get(i).get("serial"));
            List<Map<String,Object>> leisureWindowBySerial = warningMapper.findLeisureWindowBySerial(Integer.parseInt(serial.get(i).get("serial")+""));
            for (int j = 0; j < leisureWindowBySerial.size(); j++) {
                List<Map<String,Object>> endTimeAndNumberByCounter = warningMapper.findEndTimeAndNumberByCounter((Integer) leisureWindowBySerial.get(j).get("counter"));
                for (int k = 0; k < endTimeAndNumberByCounter.size(); k++) {
                    String doTime = (String) serial.get(i).get("doTime");
                    String endtime = (String) endTimeAndNumberByCounter.get(k).get("endtime");
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    try {
                        //计算出窗口最后一个件与排队区号第一个的时间差
                        long l = (sdf.parse(doTime).getTime() - sdf.parse(endtime).getTime()) / 1000 / 60;
                        if(l>10){
                            Map<String,Object> map=new HashMap<>();
                            map.put("counter", leisureWindowBySerial.get(j).get("counter"));
                            map.put("serverId", leisureWindowBySerial.get(j).get("server"));
                            map.put("queueNumber",endTimeAndNumberByCounter.get(k).get("number"));
                            map.put("warning",sdf.format(new Date()));
                             count = warningMapper.insertWarning(map);

                        }

                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }

            }
        }
        return count+"";
    }
}
